package com.cmii.sjw.department.control.jsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmii.sjw.department.control.jsc.mapper.UniversityAppActivityMapper;
import com.cmii.sjw.department.control.jsc.model.UniversityAppActivity;
import com.cmii.sjw.department.control.jsc.model.UniversityDataCollection;
import com.cmii.sjw.department.control.jsc.model.UniversityDigitalization;
import com.cmii.sjw.department.control.jsc.model.dto.UniversityAppActivityDTO;
import com.cmii.sjw.department.control.jsc.model.dto.UniversityOverviewDTO;
import com.cmii.sjw.department.control.jsc.service.UniversityAppActivityService;
import com.cmii.sjw.department.control.jsc.service.UniversityDataCollectionService;
import com.cmii.sjw.department.control.jsc.service.UniversityDigitalizationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Optional;


@Slf4j
@Service("UniversityAppActivityService")
public class UniversityAppActivityServiceImpl extends ServiceImpl<UniversityAppActivityMapper, UniversityAppActivity> implements UniversityAppActivityService {


    @Autowired
    private UniversityDataCollectionService universityDataCollectionService;

    @Autowired
    private UniversityDigitalizationService universityDigitalizationService;

    /**
     * 高校应用场景使用活跃度统计表
     *
     * @return 列表
     */
    @Override
    public List<UniversityAppActivity> list() {
        LambdaQueryWrapper<UniversityAppActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(UniversityAppActivity::getTotalScore);
        return list(wrapper);
    }


    /**
     * 应用场景使用活跃度统计
     *
     * @return 统计数据
     */
    @Override
    public UniversityAppActivityDTO getAppActivityStats() {
        List<UniversityAppActivity> list = this.list();

        int totalActiveUsers = 0;
        int totalMonthlyActiveUsers = 0;
        BigDecimal totalMonthlyRate = BigDecimal.ZERO;
        BigDecimal totalTransactionScore = BigDecimal.ZERO;
        BigDecimal totalScore = BigDecimal.ZERO;

        for (UniversityAppActivity item : list) {
            totalActiveUsers += Optional.ofNullable(item.getActiveUserCount()).orElse(0);
            totalMonthlyActiveUsers += Optional.ofNullable(item.getMonthlyActiveUsers()).orElse(0);
            totalMonthlyRate = totalMonthlyRate.add(Optional.ofNullable(item.getMonthlyActiveRate()).orElse(BigDecimal.ZERO));
            totalTransactionScore = totalTransactionScore.add(BigDecimal.valueOf(Optional.ofNullable(item.getTransactionScore()).orElse(0)));
            totalScore = totalScore.add(Optional.ofNullable(item.getTotalScore()).orElse(BigDecimal.ZERO));
        }

        UniversityAppActivityDTO stats = new UniversityAppActivityDTO();
        stats.setTotalActiveUserCount(totalActiveUsers);
        stats.setTotalMonthlyActiveUsers(totalMonthlyActiveUsers);

        int size = list.size();
        if (size > 0) {
            stats.setAverageMonthlyActiveRate(totalMonthlyRate.divide(BigDecimal.valueOf(size), 2, RoundingMode.HALF_UP));
            stats.setAverageTransactionScore(totalTransactionScore.divide(BigDecimal.valueOf(size), 2, RoundingMode.HALF_UP));
            stats.setAverageTotalScore(totalScore.divide(BigDecimal.valueOf(size), 2, RoundingMode.HALF_UP));
        } else {
            stats.setAverageMonthlyActiveRate(BigDecimal.ZERO);
            stats.setAverageTransactionScore(BigDecimal.ZERO);
            stats.setAverageTotalScore(BigDecimal.ZERO);
        }

        return stats;
    }

    /**
     * 高校所有数据统计
     *
     * @return 统计数据
     */

    @Override
    public UniversityOverviewDTO getUniversityOverviewStats() {
        UniversityOverviewDTO stats = new UniversityOverviewDTO();

        // 1. 高校数据归集与共享
        List<UniversityDataCollection> dataCollectionList = universityDataCollectionService.list();
        int total1 = dataCollectionList.size();
        int sumCatalog = 0, sumCollection = 0;
        BigDecimal totalScore1 = BigDecimal.ZERO;
        for (UniversityDataCollection item : dataCollectionList) {
            sumCatalog += Optional.ofNullable(item.getCatalogCategories()).orElse(0);
            sumCollection += Optional.ofNullable(item.getCollectionCategories()).orElse(0);
            totalScore1 = totalScore1.add(Optional.ofNullable(item.getTotalScore()).orElse(BigDecimal.ZERO));
        }
        stats.setTotalCatalogCategories(sumCatalog);
        stats.setTotalCollectionCategories(sumCollection);
        stats.setAverageTotalScore1(total1 > 0 ? totalScore1.divide(BigDecimal.valueOf(total1), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO);

        // 2. 核心业务数字化率
        List<UniversityDigitalization> digitalizationList = universityDigitalizationService.list();
        int total2 = digitalizationList.size();
        int sumCatalogCount = 0, sumDigitalized = 0;
        BigDecimal totalRate = BigDecimal.ZERO;
        for (UniversityDigitalization item : digitalizationList) {
            sumCatalogCount += Optional.ofNullable(item.getCatalogCount()).orElse(0);
            sumDigitalized += Optional.ofNullable(item.getDigitalizedCount()).orElse(0);
            totalRate = totalRate.add(Optional.ofNullable(item.getDigitalizationRate()).orElse(BigDecimal.ZERO));
        }
        stats.setTotalCatalogCount(sumCatalogCount);
        stats.setTotalDigitalizedCount(sumDigitalized);
        stats.setAverageDigitalizationRate(total2 > 0 ? totalRate.divide(BigDecimal.valueOf(total2), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO);

        // 3.应用场景使用活跃度
        List<UniversityAppActivity> appList = this.list();
        int total3 = appList.size();
        int sumActive = 0, sumMonthly = 0;
        BigDecimal totalActiveRate = BigDecimal.ZERO;
        BigDecimal totalTransactionScore = BigDecimal.ZERO;
        BigDecimal totalScore2 = BigDecimal.ZERO;
        for (UniversityAppActivity item : appList) {
            sumActive += Optional.ofNullable(item.getActiveUserCount()).orElse(0);
            sumMonthly += Optional.ofNullable(item.getMonthlyActiveUsers()).orElse(0);
            totalActiveRate = totalActiveRate.add(Optional.ofNullable(item.getMonthlyActiveRate()).orElse(BigDecimal.ZERO));
            totalTransactionScore = totalTransactionScore.add(BigDecimal.valueOf(Optional.ofNullable(item.getTransactionScore()).orElse(0)));
            totalScore2 = totalScore2.add(Optional.ofNullable(item.getTotalScore()).orElse(BigDecimal.ZERO));
        }
        stats.setTotalActiveUserCount(sumActive);
        stats.setTotalMonthlyActiveUsers(sumMonthly);
        stats.setAverageMonthlyActiveRate(total3 > 0 ? totalActiveRate.divide(BigDecimal.valueOf(total3), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO);
        stats.setAverageTransactionScore(total3 > 0 ? totalTransactionScore.divide(BigDecimal.valueOf(total3), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO);
        stats.setAverageTotalScore2(total3 > 0 ? totalScore2.divide(BigDecimal.valueOf(total3), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO);

        return stats;
    }
}
